def key = session.user.id + '-monthTranscations'
if(!memcache.contains(key)) {
	def now =  new Date() - Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
	now.clearTime()
	def tentities = datastore.execute {
		from TranscationE
		sort desc by createdTime
		where userId == session.user.id and createdTime > now
	}

	def transcationTags = [:]
	def tagTotal = [:]
	def monthTotal = 0.0
	tentities.each { transcation ->
		def ttentities = datastore.execute {
			from TranscationTagE
			sort asc by name
			where transcationId == transcation.key.id and userId == session.user.id
		}
		def tags = new StringBuilder()
		ttentities.each { tt ->
			tags += "<a class=\"table-1-link\" href=\"transcationViewForTag.groovy?transcationTag=$tt.name\">#$tt.name</a> "
			if(tagTotal[tt.name] == null) {
				tagTotal[tt.name] = 0
			}
			tagTotal[tt.name] += transcation.amount
		}
		transcationTags[transcation.key.id] = tags
		monthTotal += transcation.amount
	}
	
	key = session.user.id + '-monthTranscations'
	memcache[key] = tentities
	key = session.user.id + '-monthTagTotal'
	memcache[key] = tagTotal
	key = session.user.id + '-monthTotal'
	memcache[key] = String.format("%.2f", monthTotal)
	key = session.user.id + '-monthTranscationTags'
	memcache[key] = transcationTags
}
request.transcationTags = memcache[session.user.id + '-monthTranscationTags']
request.tagTotal = memcache[session.user.id + '-monthTagTotal']
request.total = memcache[session.user.id + '-monthTotal']
request.transcations = memcache[session.user.id + '-monthTranscations']

forward 'transcationViewForMonth.gsp'